package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_shipper_account;

import cn.com.api.service.ApiT_shipper_accountService;

import cn.com.util.*;
import cn.com.util.*;

public class T_shipper_accountBaseController {

	private static Logger logger = Logger
			.getLogger(T_shipper_accountBaseController.class);

	@Autowired
	private ApiT_shipper_accountService apiT_shipper_accountService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_shipper_account-list")
	public ModelAndView t_shipper_account_list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_shipper_account_list.jsp");

		List<ApiT_shipper_account> t_shipper_accounts = apiT_shipper_accountService
				.findT_shipper_accounts();
		mv.addObject("t_shipper_accounts", t_shipper_accounts);

		return mv;
	}

	
	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_shipper_account-add-pre")
	public ModelAndView t_shipper_account_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_shipper_account_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiT_shipper_account t_shipper_account = apiT_shipper_accountService
					.findT_shipper_accountById(Integer.valueOf(id));
			mv.addObject("t_shipper_account", t_shipper_account);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_shipper_account-add")
	public void t_shipper_account_add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String user_id = request.getParameter("user_id");
		String working_status = request.getParameter("working_status");
		String money = request.getParameter("money");
		String order_cash = request.getParameter("order_cash");
		String create_time = request.getParameter("create_time");
		String update_time = request.getParameter("update_time");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiT_shipper_account t_shipper_account = new ApiT_shipper_account();
			/*
			 * t_shipper_account.setId(id);
			 * t_shipper_account.setUser_id(user_id);
			 * t_shipper_account.setWorking_status(working_status);
			 * t_shipper_account.setMoney(money);
			 * t_shipper_account.setOrder_cash(order_cash);
			 * t_shipper_account.setCreate_time(create_time);
			 * t_shipper_account.setUpdate_time(update_time);
			 */
			apiT_shipper_accountService
					.insertT_shipper_account(t_shipper_account);
		}
		// edit
		else {
			ApiT_shipper_account t_shipper_account = apiT_shipper_accountService
					.findT_shipper_accountById(Integer.valueOf(id));
			/*
			 * t_shipper_account.setId(id);
			 * t_shipper_account.setUser_id(user_id);
			 * t_shipper_account.setWorking_status(working_status);
			 * t_shipper_account.setMoney(money);
			 * t_shipper_account.setOrder_cash(order_cash);
			 * t_shipper_account.setCreate_time(create_time);
			 * t_shipper_account.setUpdate_time(update_time);
			 */
			apiT_shipper_accountService
					.updateT_shipper_account(t_shipper_account);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_shipper_account-delete")
	public void t_shipper_account_delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiT_shipper_accountService.deleteT_shipper_account(Integer
				.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
